package com.techwells.taco.networkaccessor;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.utils.LogUtil;


public class PayNetworkAccess {
	final static String TAG = "PayNetworkAccess";

	/***
	 * 
	 * @param url
	 * @param params
	 * @return 如果获取正确，返回订单信息， 如果获取异常， 返回接口的错误描述
	 */
	public static NetworkResponse Pay (String url, Map<String, String> params) {
		final NetworkResponse resultResponse = new NetworkResponse();
		
		try {
			// 封装请求参数
			List<NameValuePair> pair = new ArrayList<NameValuePair>();
			if (params != null && !params.isEmpty()) {
				for (Map.Entry<String, String> entry : params.entrySet()) {
					pair.add(new BasicNameValuePair(entry.getKey(), entry
							.getValue()));
				}
			}
			// 把请求参数变成请求体部分
			UrlEncodedFormEntity uee = new UrlEncodedFormEntity(pair, "utf-8");
			// 使用HttpPost对象设置发送的URL路径
			HttpPost post = new HttpPost(url);
			// 发送请求体
			post.setEntity(uee);
			// 创建一个浏览器对象，以把POST对象向服务器发送，并返回响应消息
			DefaultHttpClient dhc = new DefaultHttpClient();
			HttpResponse response = dhc.execute(post);

			String respData = EntityUtils.toString(response.getEntity());
			
			if (response.getStatusLine().getStatusCode() == 200) {

				LogUtil.d(TAG, "Request:" + url);
				LogUtil.d(TAG, "Params:" + params.toString());
				LogUtil.d(TAG, "Desc:" + respData);
				
				resultResponse.setWithData(respData, 
						ServiceMediator.Service_Return_Success, 
						ServiceMediator.Service_Return_Success_Desc);
				
				return resultResponse;
			}else{

				//用以记录接口错误描述
				String respDesc = "";
				
				//错误描述在 h1 标签内
				String h1Start = "<h1>";
				String h1End = "</h1>";
				
				//定位错误描述位置
				int startIndex = respData.indexOf(h1Start);
				int endIndex = respData.indexOf(h1End);
				
				if (startIndex > 0 && endIndex > 0)
					respDesc = respData.substring(startIndex + h1Start.length(), endIndex);

				LogUtil.e(TAG, "Request:" + url);
				LogUtil.e(TAG, "Params:" + params.toString());
				LogUtil.e(TAG, "Desc:" + respDesc);
				
				
				//设置错误结果， 并添加接口错误描述
				resultResponse.setWithData(respDesc, 
						ServiceMediator.Service_Return_Error, 
						ServiceMediator.Service_Return_Error_Desc);
				
				return resultResponse;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultResponse;
	}
}
